<template>
  <div class="comment-item">
    <!-- 当前评论 -->
    <div class="comment-main">
      <!-- 头部信息 -->
      <div class="comment-header">
        <div class="user-info">
          <el-avatar :size="depth > 1 ? 24 : 32" :src="comment.headImg" />
          <span class="comment-author">{{ comment.username || '匿名用户' }}</span>
          <span v-if="comment.replyToUsername" class="reply-to">
            回复 <span class="replied-user">{{ comment.replyToUsername }}</span>
          </span>
        </div>
        <span class="comment-time">{{ parseTime(comment.createTime) }}</span>
      </div>

      <!-- 评论内容 -->
      <div class="comment-content">{{ cleanHtml(comment.content) }}</div>

      <!-- 操作按钮 -->
      <div class="comment-actions">
        <el-button type="text" @click="showReply">回复</el-button>
        <el-button 
          v-if="String(comment.sys_user_id) === String(userStore.id)"
          type="text" 
          class="delete-btn"
          @click="$emit('delete', comment)">
          删除
        </el-button>
      </div>

      <!-- 回复输入框 -->
      <div v-if="showInput" class="reply-input">
        <el-input
          v-model="replyContent"
          type="textarea"
          :rows="2"
          :placeholder="`回复 ${comment.username}：`"
        />
        <div class="reply-actions">
          <el-button size="small" @click="cancelReply">取消</el-button>
          <el-button type="primary" size="small" @click="submitReply">发送</el-button>
        </div>
      </div>
    </div>

    <!-- 递归子评论 -->
    <div 
      v-if="comment.children && comment.children.length > 0"
      class="reply-list"
      :style="{ marginLeft: `${depth * 30}px` }">
      <CommentItem
        v-for="child in comment.children"
        :key="child.commentId"
        :comment="child"
        :depth="depth + 1"
        @reply="handleChildReply"
        @delete="$emit('delete', $event)"
      />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import useUserStore from '@/store/modules/user'

const props = defineProps({
  comment: Object,
  depth: { type: Number, default: 1 }
})

const emit = defineEmits(['reply', 'delete'])

const userStore = useUserStore()
const showInput = ref(false)
const replyContent = ref('')

// 添加清理HTML标签的函数
const cleanHtml = (html) => {
  if (!html) return ''
  // 移除所有HTML标签，保留文本内容
  return html.replace(/<[^>]*>/g, '')
}

const showReply = () => {
  showInput.value = true
}

const cancelReply = () => {
  showInput.value = false
  replyContent.value = ''
}

const submitReply = () => {
  if (!replyContent.value.trim()) return
  emit('reply', {
    parent: props.comment,
    content: replyContent.value
  })
  cancelReply()
}

const handleChildReply = (payload) => {
  emit('reply', payload)
}
</script>

<style lang="scss" scoped>
.info-header {
  margin-bottom: 20px;
  padding: 12px;
  background: #f8f8f8;
  border-radius: 6px;
}

.meta-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 14px;
}

.meta-item:last-child {
  margin-bottom: 0;
}

.label {
  color: #666;
  min-width: 70px;
  margin-right: 12px;
}

.value {
  color: #333;
}
.circle {
  width: 50px;         /* 容器宽度匹配点击区域 */
  height: 50px;        /* 容器高度匹配点击区域 */
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease; /* 添加过渡效果 */

  .img-box {
    width: 40px;       /* 匹配实际图片尺寸 */
    height: 40px;      /* 匹配实际图片尺寸 */
    display: flex;
    justify-content: center;
    align-items: center;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;

    & img {
      width: 100%;     /* 使用百分比自适应容器 */
      height: 100%;    /* 使用百分比自适应容器 */
      object-fit: contain; /* 保持图片比例 */
      transition: transform 0.3s ease; /* 添加图标过渡 */
    }
  }

  /* 点击态效果 */
  &:active {
    transform: scale(0.9);
  }
}

.check {
  /* 移除原阴影动画 */
}

.img-box-check {
  animation: pulse 0.5s ease-in-out;

  img {
    transform: scale(1.1); /* 常驻放大效果 */
  }
}

/* 优化动画关键帧 */
@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1.1);
  }
}

.comment-section {
  margin-top: 30px;
  border-top: 1px solid #eee;
  padding-top: 20px;

  h3 {
    margin-bottom: 20px;
    color: #333;
  }
}

.comment-input {
  margin-bottom: 30px;
}

.comment-list {
  .comment-item {
    padding: 15px 0;
    border-bottom: 1px solid #eee;

    &:last-child {
      border-bottom: none;
    }
  }

  .comment-main {
    margin-bottom: 10px;
  }

  .comment-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .comment-author {
    font-weight: bold;
    color: #333;
  }

  .comment-time {
    color: #999;
    font-size: 12px;
  }

  .comment-content {
    color: #666;
    line-height: 1.6;
    margin-left: 40px;
  }

  .comment-actions {
    margin-left: 40px;
    margin-top: 8px;
    display: flex;
    gap: 16px;

    .delete-btn {
      color: #f56c6c;

      &:hover {
        color: #f78989;
      }
    }
  }

  .reply-input {
    margin: 10px 0 10px 40px;

    .reply-actions {
      margin-top: 8px;
      text-align: right;

      .el-button {
        margin-left: 10px;
      }
    }
  }

  .reply-list {
    margin-left: 40px;
    background: #f8f8f8;
    border-radius: 4px;
    padding: 10px;

    .reply-item {
      padding: 8px 0;

      &:not(:last-child) {
        border-bottom: 1px solid #eee;
      }

      .comment-content {
        margin-left: 32px;
      }

      .comment-actions {
        margin-left: 32px;
        margin-top: 4px;
      }

      .reply-input {
        margin: 8px 0 8px 32px;
      }

      .user-info {
        display: flex;
        align-items: center;
        gap: 8px;

        .reply-to {
          color: #999;
          font-size: 14px;

          .replied-user {
            color: #409EFF;
            font-weight: 500;
          }
        }
      }
    }
  }

  .no-comment {
    text-align: center;
    color: #999;
    padding: 20px 0;
  }
}
.reply-list {
  border-left: 2px solid #eee;
  margin-top: 8px;
  transition: margin 0.3s ease;

  .comment-item {
    position: relative;
    &::before {
      content: '';
      position: absolute;
      left: -15px;
      top: 20px;
      width: 10px;
      height: 2px;
      background: #eee;
    }
  }
}
</style>
